package com.aircms.controller.admin;

import com.aircms.common.ServerResponse;
import com.aircms.controller.BaseAdminController;
import com.aircms.pojo.News;
import com.aircms.pojo.validation.BaseQueryFieldsValidation;
import com.aircms.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author air
 * @create 2018-09-22 下午6:41
 */
@RestController
@RequestMapping(value = "api/admin/auth/news")
public class NewsAdminController extends BaseAdminController {
    @Autowired
    private INewsService iNewsService;

    /**
     * Display a listing of the resource.
     *
     * @return
     */
    @GetMapping(value = (""))
    public ServerResponse index(BaseQueryFieldsValidation baseQueryFields) throws Exception {
        try {
            return iNewsService.index(baseQueryFields,getPageNum(),getPageSize());
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return
     */
    @PostMapping(value = "")
    public ServerResponse store(@RequestBody News news) throws Exception {
        try {
            return iNewsService.store(news);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


    /**
     * Display the specified resource.
     *
     * @param id
     * @return
     */
    @GetMapping(value = "{id}")
    public ServerResponse show(@PathVariable Integer id) throws Exception {
        try {
            return iNewsService.show(id);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


    /**
     * Update the specified resource in storage.
     *
     * @return
     */
    @RequestMapping(value = "{id}", method = {RequestMethod.PUT, RequestMethod.PATCH})
    public ServerResponse update(@PathVariable Integer id,News news) throws Exception {
        try {
            return iNewsService.update(news);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "{id}")
    public ServerResponse destroy(@PathVariable Integer id) throws Exception {
        try {
            return iNewsService.destroy(id);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }


}
